﻿using Microsoft.AspNetCore.Mvc;
using New.Models.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace New.Controllers
{
    public class TestController : Controller
    {
        private readonly newContext _db;
        public TestController(newContext db)
        {
            _db = db;
        }
        [Route("/")]
        public IActionResult Index()
        {
          
            return View();
        }
       
        [Route("/detail.html")]
        public IActionResult detail()
        {
            return View();
        }
        public IActionResult AddRange()
        {
            //new一个对象列表
            var list = new List<Category>();
            list.Add(new Category
            {
                Name = "新闻"
            });
            //添加数据
            _db.Categories.AddRange(list);

            //保存 执行SQL
            var row = _db.SaveChanges();

            //如果影响行数>0
            if (row > 0)
                return Content("添加成功");
            return Content("添加失败");
        }
        public IActionResult GetList()
        {
            var list = _db.Categories.Where(x => x.Name == "新闻" +
            "").ToList();
            return new ObjectResult(list);
        }
        [Route("/Add")]
        public IActionResult GotList()
        {
            var list = new Models.Database.New()
            {
                Cateid = 2,
                Title = "string",
                Author = "作者",
                Date = DateTime.Now,
                Content = "内容"
            };
            _db.News.Add(list);
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return Content("添加成功");
            }
            return Content("添加失败");

        }
    }
}
